<?php
namespace haibao\weixin\business;

abstract class Base{
    
    /* 微信管理 */
    const FUNC_CODE_WEIXIN_LIST = 114200;
    const FUNC_CODE_WEIXIN_ADD = 114201;
    const FUNC_CODE_WEIXIN_EDIT = 114202;
    const FUNC_CODE_WEIXIN_MENU_ADD = 114203;
    const FUNC_CODE_WEIXIN_MENU_EDIT = 114204;
    const FUNC_CODE_WEIXIN_MENU_DELETE = 114205;
    const FUNC_CODE_WEIXIN_KEYWORDS_DELETE = 114206;
    const FUNC_CODE_WEIXIN_KEYWORDS_ADD = 114207;
    const FUNC_CODE_WEIXIN_KEYWORDS_EDIT = 114208;
    const FUNC_CODE_WEIXIN_MENU_LIST = 114209;
    const FUNC_CODE_WEIXIN_KEYWORDS_LIST = 114212;
    const FUNC_CODE_WEIXIN_GRAPHIC_MESSAGE = 114213;
    
    const FUNC_CODE_WEIXIN_ARTICLE_LIST = 114214;
    const FUNC_CODE_WEIXIN_ARTICLE_EDIT = 114215;
    const FUNC_CODE_WEIXIN_ARTICLE_DELETE = 114216;
    
    const FUNC_CODE_WEIXIN_OUTSOURCE = 114218;
    const FUNC_CODE_WEIXIN_OUTSOURCE_EDIT = 114219;
    const FUNC_CODE_WEIXIN_OUTSOURCE_DEL = 114220;
    const FUNC_CODE_WEIXIN_OUTSOURCE_EXPORT = 114221;
    
    private static $currentUser = null;
    
    public function __construct(){}
    
    public static function setCurrentUser($userModel){
        self::$currentUser = $userModel;
    }
    
    public static function getCurrentUser(){
        return self::$currentUser;
    } 
    public static function allowOperation($funcCode,$userPermission=\haibao\cms\model\data\User::PERMISSION_EMPLOYEE,$userId=null){
        $userModel = null;
        if ($userId){
            $userModel = \haibao\cms\business\User::getUserById($userId);
            if (!$userModel){
                return false;
            }
        }
        else{
            $userModel = self::getCurrentUser();
        }
        $allow = false;
        if($userModel){
            if ($userModel->Permission == \haibao\cms\model\data\User::PERMISSION_ADMIN){
                return true;
            }
            	
            switch ($userPermission){
                case \haibao\cms\model\data\User::PERMISSION_EMPLOYEE:
                    break;
                case \haibao\cms\model\data\User::PERMISSION_MANAGER:
                    if ($userModel->Permission == \haibao\cms\model\data\User::PERMISSION_EMPLOYEE){
                        return false;
                    }
                    break;
                case \haibao\cms\model\data\User::PERMISSION_ADMIN:
                    if ($userModel->Permission != \haibao\cms\model\data\User::PERMISSION_ADMIN){
                        return false;
                    }
                    break;
            }
            	
            foreach($userModel->Groups as $groupId){
                $funcCodes = UserGroup::getFuncCodesByGroupId($groupId);
                if (in_array($funcCode, $funcCodes)){
                    $allow = true;
                    break;
                }
            }
        }
    
        return $allow;
    }
    public static function checkOperation($funcCode,$userPermission=\haibao\cms\model\data\User::PERMISSION_EMPLOYEE,$userId=null){
        if (!self::allowOperation($funcCode,$userPermission,$userId)){
            throw new BusinessException('对不起，你无此权限，请联系系统管理员！',self::ACCESS_DENIED);
        }
    }
}
